<?php
/**
 * 商户积分管理
 * @author Administrator
 *
 */
class shopIntegralManage extends CControllerManageBase{
	public function show()
	{
		$model = new shopModel();
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;
		$show_status = $this->getRequestData("sel_shop_status","get");
		$province = $this->getRequestData("sel_province","get");
		$city = $this->getRequestData("sel_city","get");
		$area = $this->getRequestData("sel_area","get");
		$shop_name = $this->getRequestData("txt_shop_name","get");
		
		$urlParam = array();
		$where = "del_flag=0";
		
		if($province)
		{
			$where .= " and province='".$province."'";
			
			$urlParam["sel_province"] = $province;
		}
		
		if($city)
		{
			$where .= " and city='".$city."'";
			
			$urlParam["sel_city"] = $city;
		}
		
		if($area)
		{
			$where .= " and area='".$area."'";
			
			$urlParam["sel_area"] = $area;
		}
		
		if($show_status)
		{
			$where .= " and shop_status=".$show_status."";
			
			$urlParam["sel_shop_status"] = $show_status;
		}
		
		if($shop_name)
		{
			$where .= " and (shop_name like '%".$shop_name."%' or shop_simple_name like '%".$shop_name."%')";
			
			$urlParam["txt_shop_name"] = $shop_name;
		}
		
		$this->setViewParam("shop_name", $shop_name);
		
		$pageUrl = $this->getUrlParam($urlParam, array("txt_shop_name"));
		$pageUrl = $pageUrl? "?".$pageUrl:"";

		$list = $model->selectPage($pageIndex,$pageSize,"shop_simple_name,user_name,shop_id",$where,"shop_id desc","",$c,"t_shop");
		
		$pageStr = CPage::showPage($pageIndex,$pageSize,$c,$pageUrl);

		$shopIDS = "";
		foreach($list as $item)
		{
			$shopID = $item["shop_id"];
			
			$shopIDS .= $shopIDS==""?$shopID:",".$shopID;
		}

		$tjInfo = $model->getSumData($shopIDS);

		foreach($list as &$item)
		{
			$shopID = $item["shop_id"];
			$isExist = false;
			foreach($tjInfo as $v)
			{
				if($v["shop_id"] == $shopID)
				{
					$isExist = true;
					$item["add_sum"] = $v["add_sum"];
					$item["reduce_sum"] = $v["reduce_sum"];
					$item["cancel_sum"] = $v["cancel_sum"];
					break;
				}
			}
			
			if(!$isExist)
			{
				$item["add_sum"] = 0;
				$item["reduce_sum"] = 0;
				$item["cancel_sum"] = 0;
			}
		}

		$this->setJsBlock("init('".$province."','".$city."','".$area."',1);");
		$this->setMenu("商家积分查询");
		$pageUrl = $pageUrl?"&page=".$pageIndex:"page=".$pageIndex;

		$this->render("shopIntegral/list",array("list"=>$list,"pageUrl"=>$pageUrl,"pageStr"=>$pageStr,"show_status"=>$show_status));
	}
	
	public function addDetail()
	{
		$this->detailOP(1);
	}
	
	public function reduceDetail()
	{
		$this->detailOP(2);
	}
	
	public function cancelDetail()
	{
		$this->detailOP(3);
	}
	
	private function detailOP($type)
	{
		$shopID = $this->getRequestData("shopID","GET");
		
		if(!CCommon::checkNum($shopID))
		{
			$this->sysMsg("参数错误!",1);
		}
		
		$menu = $type==1?"增加":($type==2?"扣减":"撤销");
		
		$this->setMenu(array(array("text"=>"商家积分查询","href"=>$this->_manageDomain."shopIntegralManage/show/"),array("text"=>"商户".$menu."积分记录")));
		
		
		$urlParam = array();
		
		$s_time=$this->getRequestData("txt_s_time","get",date("Y-m"."-01"));
		$e_time=$this->getRequestData("txt_e_time","get",date("Y-m-d"));
		$owner_card=$this->getRequestData("txt_card_value","get","");
		
		$e_time_old = $e_time;
		$e_time = date("Y-m-d",strtotime($e_time." +1 day"));

		$where = "shop_id=".$shopID." and cost_type=".$type." and cost_time>to_date('".$s_time."','yyyy-mm-dd hh24:mi:ss')";
		$where .= " and cost_time<=to_date('".$e_time."','yyyy-mm-dd hh24:mi:ss')";
		
		$urlParam["shopID"] = $shopID;
		$urlParam["txt_s_time"] = $s_time;
		$urlParam["txt_e_time"] = $e_time;
		
		if($owner_card)
		{
			$where .= " and owner_card='".$owner_card."'";
			
			$urlParam["txt_card_value"] = $owner_card;
		}
		
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;
		$icModel = new integralChangeModel();
		
		$reportData = $icModel->getTjData($where,$type);
		
		$pageUrl = $this->getUrlParam($urlParam, array("txt_card_value"));
		$pageUrl = $pageUrl? "?".$pageUrl:"";
		
		$list = $icModel->selectPage($pageIndex,$pageSize,
			"c_id,s_id,pay_id,cancel_cost_type,cost_money,cost_type,cost_time,discount_type,discount_num,owner_card,cost_shop_before_money,cost_shop_after_money,cost_money_t,owner_name,is_cancel",
		"c.owner_id=o.owner_id and ".$where,"c_id desc","",$c,"t_integral_change c, t_owner o");
		$pageStr = CPage::showPage($pageIndex,$pageSize,$c,$pageUrl);
		
		$this->setJsFile(array("/My97DatePicker/WdatePicker.js"));
		
		$v = $type==1?"addDetail":($type==2?"reduceDetail":"cancelDetail");
		
		$this->setCssBlock(".container{min-width:1100px; width:100%; margin-right:30px;}");
		$this->render("shopIntegral/".$v,array("list"=>$list,"s_time"=>$s_time,"shop_id"=>$shopID,
			"e_time"=>$e_time_old,"owner_card"=>$owner_card,"pageStr"=>$pageStr,"reportData"=>$reportData
		));
	}
}